package com.tbsoft.trail.route;

import static org.apache.camel.util.Time.seconds;

import org.apache.camel.bam.ActivityBuilder;
import org.springframework.orm.jpa.JpaTemplate;
import org.springframework.transaction.support.TransactionTemplate;

import com.tbsoft.core.process.AbstractProcess;

/**
 * @desc BAM 监控路由
 * @author tudongyun
 * 
 */
public class BAMRouteBuilder extends org.apache.camel.bam.ProcessBuilder {

	@SuppressWarnings("deprecation")
	public BAMRouteBuilder(JpaTemplate jpaTemplate,
			TransactionTemplate transactionTemplate) {
		super(jpaTemplate, transactionTemplate);
	}

	@Override
	public void configure() throws Exception {
		// 定义监控点
		ActivityBuilder activityA = activity("direct:monitor-A").name(
				"monitor-A").correlate(header(AbstractProcess.MSG_CORRELATE_ID));
		ActivityBuilder activityB = activity("direct:monitor-B").name(
				"monitor-B").correlate(header(AbstractProcess.MSG_CORRELATE_ID));
		ActivityBuilder activityC = activity("direct:monitor-C").name(
				"monitor-C").correlate(header(AbstractProcess.MSG_CORRELATE_ID));
		//定义BAM 规则
		activityB.starts().after(activityA.completes())
				.expectWithin(seconds(1)).errorIfOver(seconds(5))
				.to("stream:err");

		activityC.starts().after(activityB.completes())
				.expectWithin(seconds(1)).errorIfOver(seconds(5))
				.to("stream:err");
	}
}
